<?php

if (!defined('IN_XLP')) {
    exit('Access Denied!');
}

/**
 * Description of wxmessage
 * 模拟微信服务器推送消息
 * @author xlp
 */
class wxmessage {

    protected static $sendUrl = null;
    protected static $token = null;

    static function init($url, $token) {
        self::$sendUrl = $url;
        self::$token = $token;
    }

    static function send($xmlData, $type = '') {
        self::$sendUrl = self::getUrl();
        if ($type == 'text') {
            $xmlData = self::getTextData($xmlData);
        }
//        saveLog('print/send', $xmlData);
        return self::setData($xmlData);
    }

    static function getTextData($data) {
        $Content = '<xml>
    <ToUserName><![CDATA[' . $data['ToUserName'] . ']]></ToUserName>
    <FromUserName><![CDATA[' . $data['FromUserName'] . ']]></FromUserName>
    <CreateTime>' . $data['CreateTime'] . '</CreateTime>
    <MsgType><![CDATA[text]]></MsgType>
    <Content><![CDATA[' . $data['Content'] . ']]></Content>
    <MsgId>' . (isset($data['MsgId']) ? $data['MsgId'] : 0) . '</MsgId>
    </xml>';
        return $Content;
    }

    static private function getUrl() {
        $url = '';
        $queryArr = array();
        $queryStr = parse_url(self::$sendUrl);
        if (isHave($queryStr['query'])) {
            parse_str($queryStr['query'], $queryArr);
        }
        $queryArr['timestamp'] = TIME;
        $queryArr['nonce'] = getRandInt(10, 1);
        $queryArr['signature'] = self::signature($queryArr['timestamp'], $queryArr['nonce']);
//        print_r($queryArr);exit;
        if (isHave($queryStr['scheme'])) {
            $url.=$queryStr['scheme'] . '://';
        } else {
            $url.='http://';
        }
        if (isHave($queryStr['host'])) {
            $url.=$queryStr['host'];
        }
        if (isHave($queryStr['user'])) {
            $url.=$queryStr['user'];
        }
        if (isHave($queryStr['pass'])) {
            $url.=$queryStr['pass'];
        }
        if (isHave($queryStr['path'])) {
            $url.=$queryStr['path'];
        }
        if ($queryArr) {
            $url.='?' . http_build_query($queryArr, '', '&');
        }
        if (isHave($queryStr['fragment'])) {
            $url.='#' . $queryStr['fragment'];
        }
//        echo $url;exit;
        return $url;
    }

    static private function setData($xmlData) {
        $header = array(
            'Content-type: text/xml'//定义content-type为xml,注意是数组
        );
        $ch = curl_init(self::$sendUrl);
        curl_setopt($ch, CURLOPT_URL, self::$sendUrl);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
        curl_setopt($ch, CURLOPT_TIMEOUT, 5); // 设置超时限制防止死循环
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $xmlData);
        $response = curl_exec($ch);
        if (curl_errno($ch)) {
            showError(curl_error($ch));
        }
        curl_close($ch);
        return $response;
    }

    static private function signature($timestamp, $nonce) {
        $tmpArr = array(self::$token, $timestamp, $nonce);
        sort($tmpArr, SORT_STRING);
        $tmpStr = implode($tmpArr);
        return sha1($tmpStr);
    }

}
